<template>
  <div class="sick">
    <el-card shadow="never">
      <div class="my-card">
        <el-form ref="form" :model="form1"  :label-position="labelPosition">
          <el-form-item>
            <el-input placeholder="请输入医院名称" v-model="input" class="input-with-select" style="width: 450px">
              <el-button slot="append" icon="el-icon-search" @click="dofindLike"></el-button>
            </el-input>
          </el-form-item>
        </el-form>
        <div class="btn-event">
          <el-button @click="addUI" type="primary" class="left">添加</el-button>
          <el-button @click="updateTable" type="info" class="left">重置</el-button>
          <el-button @click="importxls" class="left" type="success" icon="el-icon-upload2">导入</el-button>
          <el-button @click="exportxls" class="left" type="warning" icon="el-icon-download">导出</el-button>
        </div>
      </div>

      <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" height="480" row-key="permissionId" border
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="deptId" label="科室Id" sortable></el-table-column>
        <el-table-column prop="deptName" label="科室名称" sortable></el-table-column>
        <el-table-column prop="hName" label="医院名称" sortable></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- <el-button type="primary" >详情</el-button> -->
            <el-button type="info" @click="updateUI(scope.row)" plain size="mini">修改</el-button>
            <el-button type="danger" @click="del(scope.row)" plain size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="科室" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="科室名称：" :label-width="formLabelWidth">
            <el-input v-model="form.deptName" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item v-if="form.deptId == null" label="所在医院：" :label-width="formLabelWidth">
            <el-select v-model="form.hId" placeholder="请选择医院">
              <el-option v-for="item in options" :key="item.hId" :label="item.hName" :value="item.hId"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
        </div>
      </el-dialog>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.page"
        :page-sizes="[5, 10, 15, 20]" :page-size="pageInfo.size" layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"></el-pagination>

      <el-dialog title="批量导入" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-upload class="upload-demo" :file-list="fileList" drag action="https://jsonplaceholder.typicode.com/posts/"
          multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处,或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">只能上传xls文件</div>
        </el-upload>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="uploadxls()">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  findAll,
  doimportxls,
  doexportxls,
  doSaveOrUpdate,
  dodel,
  findLike,
  find
} from "@/api/dept/index.js";
import { findAllHospital, findAllDept } from "@/api/sickroom/index.js";

export default {
  mounted() {
    this.updateTable();
    findAllHospital().then(r => {
      this.options1 = r.data;
    });
  },
  data() {
    return {
      labelPosition: 'left',
      form: {},
      form1: {},
      tableData: [],
      input: "",
      dialogFormVisible: false,
      formLabelWidth: "180px",
      dialogVisible: false,
      fileList: [],
      pageInfo: {
        total: 0,
        page: 1,
        size: 5
      },
      options: [],
      options2: [],
      options1: []
    };
  },
  methods: {
    dofind() {
      find(this.form1).then(r => {
        this.tableData = r.data;
      })
      this.form1 = {};
    },
    dofindLike() {
      findLike(this.input).then(r => {
        this.tableData = r.data;
      })
      this.input = "";
    },
    dofindAllDept() {
      findAllDept(this.form1.hId).then(r => {
        this.options2 = r.data
      })
    },
    handleSizeChange(size) {
      this.pageInfo.size = size;
      this.updateTable();
    },
    handleCurrentChange(page) {
      this.pageInfo.page = page;
      this.updateTable();
    },
    saveOrUpdate() {
      doSaveOrUpdate(this.form).then(r => {
        if (r.code == 200) {
          this.dialogFormVisible = false;
          this.$message.success(r.msg);
          this.updateTable();
        }
      });
    },
    uploadxls() {
      doimportxls(this.form).then(r => { });
    },
    onSuccess() {
      this.updateTable();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    importxls() {
      this.dialogVisible = true;
    },
    async exportxls() {
      doexportxls();
    },
    addUI() {
      (this.options = []), (this.form = {});
      this.dialogFormVisible = true;
      findAllHospital().then(r => {
        this.options = r.data;
      });
    },
    updateTable() {
      findAll(this.pageInfo).then(r => {
        this.tableData = r.data.list;
        this.pageInfo.total = r.data.total;
      });
    },
    updateUI(row) {
      this.dialogFormVisible = true;
      this.form = row;
    },
    del(row) {
      this.$confirm("确认删除?")
        .then(() => {
          let deptId = row.deptId;

          del(deptId).then(r => {
            if (r.code == 200) {
              this.updateTable();
              this.$message.success(r.msg);
              return;
            }
            this.$message.error(r.msg);
          });
        })
        .catch(() => { });
    }
  }
};
</script>

<style scoped>
.el-card {
  margin-bottom: 15px;
}

.sick {

  padding: -10px !important;
}

.sick .my-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
